package com.candy.controller;

import com.candy.pojo.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
//@RequestMapping("/logincontrol")
public class LoginController {

    @RequestMapping("/show1")
    public ModelAndView show1(){
        ModelAndView mv=new ModelAndView("welcome");
        mv.addObject("msg","快乐的springmvc...");
        return mv;
    }
    @RequestMapping("/show2")
    public ModelAndView show2(){
//        创建list集合，
        List<Book> bookList=new ArrayList<>();
        for (int i = 11; i <=20 ; i++) {
            Book book=new Book();
            book.setId(i);
            book.setName("乾坤大挪移"+i);
            book.setPrice(99.9+i);
            bookList.add(book);
        }
        ModelAndView mv=new ModelAndView("bookinfo");
        mv.addObject("booklist",bookList);
        return mv;
    }

    /**
     * ResponseBody注解
     * 返回值以json格式返回
     * @return
     */
    @RequestMapping("/show3")
    @ResponseBody
    public List<Book> show3(){

        List<Book> bookList=new ArrayList<>();
        for (int i = 11; i <=20 ; i++) {
            Book book=new Book();
            book.setId(i);
            book.setName("乾坤大挪移"+i);
            book.setPrice(99.9+i);
            bookList.add(book);
        }
        return bookList;
    }
//    如果返回的是字符串，则返回值为视图的名字
    @RequestMapping("/show4")
    public String show4(){
        return "welcome";
    }
    @RequestMapping("/show5")
    public String show5(Model model){
        model.addAttribute("msg","我是一个快乐的springmvc");
        return "welcome";
    }








}
